﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Presenters;
using University.Workshop.Presenters.Views;
using University.Workshop.Model;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Web.Security;

namespace University.Workshop.WebApp
{
	public partial class SendTicket : System.Web.UI.Page, ISendTicketView
	{
		SendTicketPresenter presenter;
		Collection<PurchasedProduct> purchased;
		

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!User.Identity.IsAuthenticated)
				FormsAuthentication.RedirectToLoginPage();
			purchased = new Collection<PurchasedProduct>();
			presenter = new SendTicketPresenter(this);
			if (!IsPostBack)
			{
				Session["numPage"] = 1;
				LblMessage.Text = "";
				Session["priority"] = 1;
				Session["orderId"] = -1;
				presenter.ChangePurchasesPage(1, User.Identity.Name);
				testDL.DataSource = purchased;
				testDL.DataBind();
			}
		}

		protected void SendTicketBtn_Click(object sender, EventArgs e)
		{
			presenter.SubmitTicketInformation();
		}

		public string IssueTitle
		{
			get { return TbIssue.Text; }
		}

		public string Description
		{
			get { return txtContent.Text; }
		}

		public long OrderId
		{
			get { return long.Parse(Session["orderId"].ToString(), CultureInfo.InvariantCulture); }
		}

		public short Priority
		{
			get { return short.Parse(Session["priority"].ToString(), CultureInfo.InvariantCulture); }
		}

		public string Result
		{
			set
			{
				if (value == "Ticket sent succesfully!")
				{
					LblMessage.Text = "";
					Response.Write("<script>alert('" + value + "');window.location.href = 'Default.aspx';</script>");
				}
				else LblMessage.Text = value;
			}
			get
			{
				return LblMessage.Text;
			}
		}

		protected void NoSpecific_CheckedChanged(object sender, EventArgs e)
		{
			Session["priority"] = 1;
		}

		protected void ProductNoArrives_CheckedChanged(object sender, EventArgs e)
		{
			Session["priority"] = 4;
		}

		protected void DamagedProduct_CheckedChanged(object sender, EventArgs e)
		{
			Session["priority"] = 2;
		}

		protected void FakeProduct_CheckedChanged(object sender, EventArgs e)
		{
			Session["priority"] = 3;
		}

		protected void OtherIssue_CheckedChanged(object sender, EventArgs e)
		{
			Session["priority"] = 2;
		}

		protected void Unnamed1_Click(object sender, EventArgs e)
		{
			multiView.ActiveViewIndex = 1;
		}

		protected void CloseBtn_Click(object sender, EventArgs e)
		{
			multiView.ActiveViewIndex = 0;
		}

		private bool bSwitch = false;
		string color1 = "#ffffcc";
		string color2 = "#ccff99";

		public string TRJavaScript(Control con)
		{
			string tmp;
			DataListItem dli = con as DataListItem;
			Button btn = dli.FindControl("HiddenButton") as Button;
			string _js = "bgcolor={0} onMouseover='rowcolor=this" +
						 ".style.backgroundColor;this.style.backgroundColor" +
						 "=\"#3670C7\"; this.style.color=\"white\"; this.style.cursor = \"hand\"' " +
						 "onMouseout='this.style.backgroundColor=\"white\"; this.style.color=\"black\";' " +
						 " onclick='document.getElementById(\"{1}\").click();' ";
			tmp = bSwitch ? string.Format(_js, color1, btn.ClientID) :
						   string.Format(_js, color2, btn.ClientID);
			bSwitch = !bSwitch;
			return tmp;
		}

		protected void DL_ItemCommand(object source, DataListCommandEventArgs e)
		{
			long orderId = long.Parse(e.CommandArgument.ToString(), CultureInfo.InvariantCulture);
			Session["orderId"] = orderId;
			presenter.ChangeProductName(orderId);
			multiView.ActiveViewIndex = 0;
		}

		public Collection<PurchasedProduct> ActualPurchasesPage
		{
			get { return purchased; }
		}

		public void SetActualPurchasesPage(Collection<PurchasedProduct> actual, int max)
		{
			PrevButton.Enabled = true;
			NextButton.Enabled = true;
			purchased.Clear();
			purchased = actual;
			testDL.DataSource = purchased;
			testDL.DataBind();
			int numPage = int.Parse(Session["numPage"].ToString());
			if (numPage == 1)
				PrevButton.Enabled = false;
			if ((max-((numPage-1)*5))<5)
				NextButton.Enabled = false;
			ShowContent.InnerText = "Showing " + (((numPage - 1) * 5) + 1) + "-" + ((((numPage) * 5) > max) ? max : ((numPage) * 5)) + " of " + max;
			if (purchased.Count == 0)
			{
				testDL.Visible = false;
				ShowContent.InnerText = "You have not purcharsed any products!";
			}

		}

		protected void PrevButton_Click(object sender, EventArgs e)
		{
			int pageNum = int.Parse(Session["numPage"].ToString());
			pageNum--;
			Session["numPage"] = pageNum;
			presenter.ChangePurchasesPage(pageNum, User.Identity.Name);
		}

		protected void NextButton_Click(object sender, EventArgs e)
		{
			int pageNum = int.Parse(Session["numPage"].ToString());
			pageNum++;
			Session["numPage"] = pageNum;
			presenter.ChangePurchasesPage(pageNum, User.Identity.Name);
		}


		public string PurchasedProductName
		{
			get
			{
				return ProductName.Text;
			}
			set
			{
				ProductName.Text = value;
			}
		}
	}
}